import java.util.Stack;

public class BM11 {
    /*
    *  假设链表中每一个节点的值都在 0 - 9 之间，那么链表整体就可以代表一个整数。
给定两个这种链表，请生成代表两个整数相加值的结果链表。 */

    public static class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode addInList (ListNode head1, ListNode head2) {
        // write code here
        Stack<ListNode> stack1 = new Stack<>();
        Stack<ListNode> stack2 = new Stack<>();
        while(head1 != null){
            stack1.push(head1);
            head1 = head1.next;
        }
        while(head2 != null){
            stack2.push(head2);
            head2 = head2.next;
        }
        int a = 0; int b = 0; int i = 0;
        while(!stack1.isEmpty()){
            ListNode node = stack1.pop();
            a = a + node.val * (int)Math.pow(10, i);
            i++;
        }
        i = 0;
        while(!stack2.isEmpty()){
            ListNode node = stack2.pop();
            b = b + node.val * (int)Math.pow(10, i);
            i++;
        }
        int res = a+ b;
        System.out.println(res);
        ListNode head = new ListNode(0);
        while(res != 0){
            int tmp = res % 10;
            ListNode thisNode = new ListNode(tmp);
            thisNode.next = head.next;
            head.next = thisNode;
            res = res / 10;
        }

        return head.next;
    }
}
